package com.imti.secondmenudemo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import com.imti.secondmenudemo.LeftAdapter.LeftHolder;
import java.util.List;

public class LeftAdapter extends RecyclerView.Adapter<LeftHolder>
{

    private Context context;

    private List<LeftModel> leftData;

    private onLeftItemClickListener listener;

    public LeftAdapter(Context context)
    {
        this.context = context;
    }

    public void setLeftData(List<LeftModel> leftData)
    {
        this.leftData = leftData;
    }

    public void setListener(onLeftItemClickListener listener)
    {
        this.listener = listener;
    }

    @NonNull
    @Override
    public LeftHolder onCreateViewHolder(
        @NonNull ViewGroup parent, int viewType)
    {
        LeftHolder holder = new LeftHolder(
            LayoutInflater.from(context)
                .inflate(R.layout.left_item, null));

        holder.itemView.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (null != listener)
                {
                    listener.onLeftItemClick(holder.getAdapterPosition());
                }
            }
        });

        return holder;
    }
    @Override
    public void onBindViewHolder(
        @NonNull LeftAdapter.LeftHolder holder, int position)
    {
        holder.leftItem.setText(leftData.get(position).getName());

        //蓝色 #EDF5F8
        if (leftData.get(position).isSelect())
        {
            holder.itemView.setBackgroundColor(Color.WHITE);
            holder.leftItem.setTextColor(Color.parseColor("#69A4CD"));
            holder.leftItem.setTypeface(Typeface.DEFAULT_BOLD);
        } else
        {
            holder.itemView.setBackgroundColor(
                Color.parseColor("#EDF5F8"));
            holder.leftItem.setTextColor(Color.parseColor("#444444"));
            holder.leftItem.setTypeface(Typeface.DEFAULT);
        }

    }
    @Override
    public int getItemCount()
    {
        return null == leftData ? 0 : leftData.size();
    }

    class LeftHolder extends ViewHolder
    {

        TextView leftItem;

        public LeftHolder(@NonNull View itemView)
        {
            super(itemView);

            leftItem =
                itemView.findViewById(R.id.left_item_name);
        }
    }

    interface onLeftItemClickListener
    {

        void onLeftItemClick(int position);
    }
}
